var requestHandler = {
  params: {},
  API_URL: '',
  token: '',
  success: function (res) {
    // success  
  },
  fail: function () {
    // fail  

  } //GET请求   

};

function GET(requestHandler) {
  request('GET', requestHandler);
} //POST请求  


function POST(requestHandler) {
  request('POST', requestHandler);
}

function request(method, requestHandler) {
  //注意：可以对params加密等处理  
  let params = requestHandler.params;
  // console.log(params);

  if (params && !params.appid) {
    params.appid = uni.getStorageSync('appId');
  }
  // let data = '?';
  // if (requestHandler.data){
  //   for (let key in requestHandler.data){
  //     console.log(key, requestHandler.data[key]);
  //     let str = key + '=' + requestHandler.data[key] ;
  //     data = str.join('&');
  //   }
  // }
  // console.log(data);
  // console.log(params);

  let API_URL = requestHandler.API_URL;
  uni.request({
    url: `https://api.1lianyou.com/${API_URL}`,
    // url: `http://47.110.229.171:2022/${API_URL}`,
    data: params || '',
    method: method,
    header: {
      'content-type': 'application/json'
    },
    // 设置请求的 header  
    success: function (res) {
      if (res.statusCode == 404 || res.statusCode == 500) {
        uni.showToast({
          title: '网络错误，请稍后后再试',
          duration: 1000
        });
        return;
      }
      //注意：可以对参数解密等处理  
      // console.log('返回:',res)
      requestHandler.success(res);
    },
    fail: function (res) {
      requestHandler.fail();
      console.error(API_URL, res.data, params);
    },
    complete: function () {
      // complete 
    }
  });
}

module.exports = {
  GET: GET,
  POST: POST
};